package com.cn.transfer.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.InspectionData;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author: weidian
 * @date: 2023/11/7 13:48
 * @Description: 检验报告
 */
@RestController
@Slf4j
public class InspectionDataController {

    @GetMapping("/upload")
    public void upload(){
        String authHeaderPayload = getAuthHeaderPayload();
        Header[] headers = new Header[2];
        headers[0] = new BasicHeader("Authorization", authHeaderPayload);
        headers[1] = new BasicHeader("Content-Type", "application/json;charset=utf-8");

        Header[] originalHeaders = ConnmeUtils.createHeaders();
        String next="";
        boolean fale=true;
        do {
            JSONObject entries = ApiClient.sendGetRequest("https://jinshuju.net/api/v1/forms/SxmgLr/entries?next="+next, headers);
            String nexts = entries.getStr("next");
            String data=entries.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<InspectionData> inspectionData = JSONUtil.toList(objects, InspectionData.class);
            List<Map<String, Object>> datas = new ArrayList<>();
            for (InspectionData inspectionDatum : inspectionData) {
                Map<String, Object> parameters = new HashMap<>();
                parameters.put("app_id", "64a7b7416bbd930007630ed5");
                parameters.put("entry_id", "6549fa54e453ac99ce6bf154");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameters, originalHeaders, inspectionDatum.getSerial_number(),"serial_number");
                // 查询是否已经存在简道云
                if (jdyPurchaseOrders.size() > 0) {
                    continue;
                }
                datas.add(ConnmeUtils.convertEntityToMap(inspectionDatum));
            }
            Map<String, Object> parameter = new HashMap<>();
            parameter.put("app_id", "64a7b7416bbd930007630ed5");
            parameter.put("entry_id", "6549fa54e453ac99ce6bf154");
            parameter.put("data_list", datas);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject entries1 = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/batch_create", originalHeaders, jsonSale);
            log.info("来货报告"+entries1);
            if (nexts==null){
                fale=false;
            }else {
                next= nexts;
            }
        }while (fale);
    }

    private String getAuthHeaderPayload() {
        String credentials = "vDmEzxuOcaY1Mgqxa5JicA" + ":" + "jluI9L0mxpMgEE5TxscHrA";

        String base64EncodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());
        return "Basic " + base64EncodedCredentials;
    }
}
